package edu.wichita.weighin;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class PriceFetch {
       
    public static final String PREFS_SOURCE = "source"; //private file for sources
    private static final String PREFS_SOURCE_CNN = "cnn";
    public static final String PREFS_MATERIAL = "material"; //private file for materials
    private static final String PREFS_MATERIAL_GOLD = "gold";
    private static final String PREFS_MATERIAL_SILVER = "silver";
    private static final String PREFS_MATERIAL_PLATINUM = "platinum";
    private static final String PREFS_MATERIAL_COPPER = "copper";
    public static final String PREFS_INFO = "info"; //private file for displaying info
    private static final String PREFS_INFO_SOURCE = "s";
    private static final String PREFS_INFO_MATERIAL = "m";
    private static final String PREFS_INFO_PRICE = "p";
    private static final String PREFS_INFO_WEIGHT = "w";
       
    Context context;
       
    int i = 0;
    String url_cnn = "http://money.cnn.com/data/commodities/";
    String goldElement = "[stream=last_GCM5]";
    String silverElement = "[stream=last_SIN5]";
    String platinumElement = "[stream=last_PLN5]";
    String copperElement = "[stream=last_HGN5]";
       
    public enum Material {         
        GOLD_24K, GOLD_23K, GOLD_22K, GOLD_18K, GOLD_14K, SILVER, PLATINUM, COPPER;
    }
       
    public PriceFetch (Context con) {
               
        context = con;
               
        //creating private file of sources
        SharedPreferences sourcePref = context.getSharedPreferences(PREFS_SOURCE, Context.MODE_PRIVATE);
        SharedPreferences.Editor sourceEditor = sourcePref.edit();
        sourceEditor.putString(PREFS_SOURCE_CNN, url_cnn).commit();
            
        //creating private file of material codes
        SharedPreferences materialPref = context.getSharedPreferences(PREFS_MATERIAL, Context.MODE_PRIVATE);
        SharedPreferences.Editor materialEditor = materialPref.edit();
        materialEditor.putString(PREFS_MATERIAL_GOLD, goldElement).commit();
        materialEditor.putString(PREFS_MATERIAL_SILVER, silverElement).commit();
        materialEditor.putString(PREFS_MATERIAL_PLATINUM, platinumElement).commit();
        materialEditor.putString(PREFS_MATERIAL_COPPER, copperElement).commit();
               
        //creating private file of information involved in calculations
        SharedPreferences infoPref = context.getSharedPreferences(PREFS_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor infoEditor = infoPref.edit();
        infoEditor.putString(PREFS_INFO_SOURCE, "N/A").commit();
        infoEditor.putString(PREFS_INFO_MATERIAL, "NO METAL CHOSEN").commit();
        infoEditor.putFloat(PREFS_INFO_PRICE, 0.00f).commit();
        infoEditor.putFloat(PREFS_INFO_WEIGHT, 0.00f).commit();
    }
       
    public void pickData (String mat, float weigh) {
        
    	String newMat = mat.replaceAll(" ", "_");
    	System.out.println("WITHIN PICKDATA: " + mat);
        SharedPreferences sp = context.getSharedPreferences(PREFS_SOURCE, Context.MODE_PRIVATE);
        SharedPreferences mp = context.getSharedPreferences(PREFS_MATERIAL, Context.MODE_PRIVATE);
        SharedPreferences ip = context.getSharedPreferences(PREFS_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor ipf = ip.edit();
        String where, what;
              
        switch (Material.valueOf(newMat)) {
               
            case GOLD_24K:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_GOLD, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "GOLD 24K").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            case GOLD_23K:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_GOLD, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "GOLD 23K").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            case GOLD_22K:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_GOLD, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "GOLD 22K").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            case GOLD_18K:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_GOLD, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "GOLD 18K").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            case GOLD_14K:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_GOLD, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "GOLD 14K").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            case SILVER:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_SILVER, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "SILVER").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            case PLATINUM:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_PLATINUM, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "PLATINUM").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            case COPPER:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_COPPER, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "COPPER").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            default:
                where = sp.getString(PREFS_SOURCE_CNN, "N/A");
                what = mp.getString(PREFS_MATERIAL_COPPER, "N/A");
                ipf.putString(PREFS_INFO_SOURCE, where).commit();
                ipf.putString(PREFS_INFO_MATERIAL, "NO METAL CHOSEN").commit();
                ipf.putFloat(PREFS_INFO_WEIGHT, weigh).commit();
                new DataStream().execute(where, what);
                break;
            
        }
    }

    class DataStream extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            
            try {
                Document document = Jsoup.connect(params[0]).timeout(5000).get();
                String price = document.select(params[1]).first().text();
                return price;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                SharedPreferences pricePref = context.getSharedPreferences(PREFS_INFO, Context.MODE_PRIVATE);
                SharedPreferences.Editor priceEditor = pricePref.edit();
                priceEditor.putString(PREFS_INFO_SOURCE, "CONNECTION ERROR").commit();
                
                return null;
            }           
        }
               
        protected void onPostExecute (String value) {
                       
            float num;
            if (value != null) {
                String regex = "(?<=[\\d])(,)(?=[\\d])";
                Pattern p = Pattern.compile(regex);
                String str = value;
                Matcher m = p.matcher(str);
                str = m.replaceAll("");
                num = Float.parseFloat(str);
            } else {
                num = 999999; //error code, value will be incredibly large
            }
            SharedPreferences pricePref = context.getSharedPreferences(PREFS_INFO, Context.MODE_PRIVATE);
            SharedPreferences.Editor priceEditor = pricePref.edit();
            priceEditor.putFloat(PREFS_INFO_PRICE, num).commit();
                     
            Intent intent = new Intent(context, MetalResultsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
            context.startActivity(intent);               
        }
    }
}